package gowcage.tfgame;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;

public class MainView extends View implements Runnable, OnGestureListener {
    Map m;
    Paint p;
    Npc n[] = new Npc[100];
    Gun gun[] = new Gun[8];

    public MainView(Context context) {
        super(context);
        m = new Map();
        p = new Paint();
        for (int i = 0; i < n.length; i++) {
            n[i] = new Npc(m);
        }

        for (int i = 0; i < 4; i++) {
            gun[i] = new Gun(64 + 16, (i + 1) * 32 + 16);
            gun[i + 4] = new Gun(0 + 16, (i + 1) * 32 + 16);
        }
        new Thread(this).start();
    }

    int a;

    protected void onDraw(Canvas c) {
        //c.drawColor(0xffffff);
        m.paint(c, p);
        for (int i = 0; i < n.length; i++) {
            n[i].paint(c, p);
        }
        for (int i = 0; i < gun.length; i++) {
            gun[i].paint(c, p);
        }
        a++;
        for (int i = 0; i < n.length; i++) {
            if (a % 20 == 0 && !n[i].live) {
                n[i].live = true;
                n[i].init();
                n[i].id = 0;
                break;
            }
        }
    }

    @Override
    public void run() {
        while (true) {
            long in = System.currentTimeMillis();
            for (int i = 0; i < gun.length; i++) {
                gun[i].fire(n);
            }
            postInvalidate();
            long out = System.currentTimeMillis() - in;
            try {
                Thread.sleep(60 - out);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}
